package com.xhj.controller;

import com.xhj.entity.VideoInfoForm;
import com.xhj.service.VideoService;
import com.xhj.util.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@Api(value="章节课时管理")
@RestController
@RequestMapping("/EduAdmin/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @ApiOperation(value = "新增课时")
    @RequestMapping("add")
    public ResultEntity saveVideo(
            @ApiParam(name = "videoForm", value = "课时对象", required = true)
            @RequestBody VideoInfoForm videoInfoForm){
        videoService.saveVideoInfo(videoInfoForm);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "根据Id查询课时")
    @RequestMapping("get/{id}")
    public ResultEntity getVideoInfoById(
            @ApiParam(name = "id", value = "课时Id", required = true)
            @PathVariable String id){
        VideoInfoForm videoInfoForm = videoService.getVideoInfoById(id);
        return ResultEntity.successWithData().data("videoInfoForm",videoInfoForm);
    }

    @ApiOperation(value = "更新课时")
    @RequestMapping("update/{id}")
    public ResultEntity updateVideoInfoById(
            @ApiParam(name = "id", value = "课时Id", required = true)
            @PathVariable String id,
            @ApiParam(name = "videoInfoForm", value = "课时基本信息", required = true)
            @RequestBody VideoInfoForm videoInfoForm){
        videoService.updateVideoInfoById(videoInfoForm);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "根据Id删除课时")
    @RequestMapping("delete/{id}")
    public ResultEntity deleteVideoById(
            @ApiParam(name = "id", value = "课时Id", required = true)
            @PathVariable String id
    ){
        boolean result = videoService.deleteById(id);
        if (result) return ResultEntity.successWithoutData();
        return ResultEntity.failedWithMessage("删除课时失败！");
    }
}
